package com.hfzy.ihk.web.freeswitchManage.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

/**
 * Created by Administrator on 2016/6/7.
 * @author wws
 *
 */
public class SpringWebSocketUtils {

    private static final Logger logger = LoggerFactory.getLogger(SpringWebSocketUtils.class);

    public static String pushToClient(WebSocketSession session, String json){

        if(session == null){
            logger.info("websocket is null, json:" + json);
            return  Constants.FAIL;
        }

        String res = null;

        try {
            if(session.isOpen()){
                session.sendMessage(new TextMessage(json));
                res = Constants.SUCCESS;
            }else {
                res = Constants.FAIL;
                logger.error("pushToClient fail!webSocket is closed!json:"+json);
            }
        } catch (Exception e) {
            logger.error("pushToClient failed! data:"+json,e);
            res = Constants.FAIL;
        }

        return res;

    }

    public static String pushToSwx(WebSocketSession session, String json){
        return pushToClient(session,json);
    }
}
